﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Interfejsi;
using source;
using MySql.Data.MySqlClient;

namespace DAL
{
    partial class DAL
    {
        public class CasDAO : IDaoCrud<Cas>
        {
            #region IDaoCrud<Cas> Members
            protected MySqlCommand c;
            public long create(Cas entity)
            {
                try
                {
                    c = new MySqlCommand("insert into cas (datum, predmet_id, profesor_id, razred_id, opis, dodatne_napomene) values (@Datum, @PredmetID, @ProfesorID, @RazredID, @Opis, @DodatneNapomene);", con);
                    c.Parameters.AddWithValue("@Datum", entity.Datum);
                    c.Parameters.AddWithValue("@PredmetID", entity.PredmetID);
                    c.Parameters.AddWithValue("@ProfesorID", entity.ProfesorID);
                    c.Parameters.AddWithValue("@RazredID", entity.RazredID);
                    c.Parameters.AddWithValue("@Opis", entity.Opis);
                    c.Parameters.AddWithValue("@DodatneNapomene", entity.DodatneNapomene);
                    return c.LastInsertedId;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Cas> getAll()
            {
                try
                {
                    c = new MySqlCommand("select * from cas;", con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Cas> casovi = new List<Cas>();
                    while (r.Read())
                    {
                        Cas cas = new Cas();
                        cas.ID = r.GetInt32("id");
                        cas.Datum = r.GetDateTime("datum");
                        cas.PredmetID = r.GetInt32("predmet_id");
                        cas.ProfesorID = r.GetInt32("profesor_id");
                        cas.RazredID = r.GetInt32("razred_id");
                        cas.Opis = r.GetString("opis");
                        cas.DodatneNapomene = r.GetString("dodatne_napomene");
                        casovi.Add(cas);
                    }
                    return casovi;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Cas read(Cas entity)
            {
                try
                {
                    c = new MySqlCommand("select * from cas where id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    MySqlDataReader r = c.ExecuteReader();
                    Cas cas = new Cas();
                    if (r.Read())
                    {
                        cas.ID = r.GetInt32("id");
                        cas.Datum = r.GetDateTime("datum");
                        cas.PredmetID = r.GetInt32("predmet_id");
                        cas.ProfesorID = r.GetInt32("profesor_id");
                        cas.RazredID = r.GetInt32("razred_id");
                        cas.Opis = r.GetString("opis");
                        cas.DodatneNapomene = r.GetString("dodatne_napomene");
                    }
                    return cas;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Cas update(Cas entity)
            {
                try
                {
                    c = new MySqlCommand("UPDATE cas SET datum = @Datum, predmet_id = @PredmetID, profesor_id = @ProfesorID razred_id = @RazredID, opis = @Opis, dodatne_napomene = @DodatneNapomene where id = @ID;", con);
                    c.Parameters.AddWithValue("@Datum", entity.Datum);
                    c.Parameters.AddWithValue("@PredmetID", entity.PredmetID);
                    c.Parameters.AddWithValue("@ProfesorID", entity.ProfesorID);
                    c.Parameters.AddWithValue("@RazredID", entity.RazredID);
                    c.Parameters.AddWithValue("@Opis", entity.Opis);
                    c.Parameters.AddWithValue("@DodatneNapomene", entity.DodatneNapomene);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    c.ExecuteNonQuery();
                    return entity;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public void delete(Cas entity)
            {
                try
                {
                    c = new MySqlCommand("delete from cas where id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    c.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Cas getById(int id)
            {
                try
                {
                    c = new MySqlCommand("select * from cas where id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", id);
                    MySqlDataReader r = c.ExecuteReader();
                    Cas cas = new Cas();
                    if (r.Read())
                    {
                        cas.ID = r.GetInt32("id");
                        cas.Datum = r.GetDateTime("datum");
                        cas.PredmetID = r.GetInt32("predmet_id");
                        cas.ProfesorID = r.GetInt32("profesor_id");
                        cas.RazredID = r.GetInt32("razred_id");
                        cas.Opis = r.GetString("opis");
                        cas.DodatneNapomene = r.GetString("dodatne_napomene");
                    }
                    return cas;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Cas> getByExample(string name, string value)
            {
                try
                {
                    c = new MySqlCommand("select * from cas where @Kolona = @Vrijednost;", con);
                    c.Parameters.AddWithValue("@Kolona", name);
                    c.Parameters.AddWithValue("@Vrijednost", value);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Cas> casovi = new List<Cas>();
                    while (r.Read())
                    {
                        Cas cas = new Cas();
                        cas.ID = r.GetInt32("id");
                        cas.Datum = r.GetDateTime("datum");
                        cas.PredmetID = r.GetInt32("predmet_id");
                        cas.ProfesorID = r.GetInt32("profesor_id");
                        cas.RazredID = r.GetInt32("razred_id");
                        cas.Opis = r.GetString("opis");
                        cas.DodatneNapomene = r.GetString("dodatne_napomene");
                        casovi.Add(cas);
                    }
                    return casovi;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            #endregion
        }
    }
}
